package com.runrunfast.fxj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.runrunfast.fxj.entity.SysRole;
import com.runrunfast.fxj.entity.SysRoleResources;
import com.runrunfast.fxj.entity.SysUser;
import com.runrunfast.fxj.enums.UserStatusEnum;
import com.runrunfast.fxj.exception.FxjException;
import com.runrunfast.fxj.mapper.SysRoleMapper;
import com.runrunfast.fxj.mapper.SysRoleResourcesMapper;
import com.runrunfast.fxj.service.ISysRoleResourcesService;
import com.runrunfast.fxj.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.runrunfast.fxj.utils.LevelUtil;
import com.runrunfast.fxj.utils.ResultVOUtil;
import com.runrunfast.fxj.vo.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 陈胤训
 * @since 2019-03-12
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private SysRoleMapper RoleMapper;

    @Autowired
    private ISysRoleResourcesService roleResourcesService;

    @Override
    public List<SysRole> listRolesByUserId(Integer userId) {
        return baseMapper.listRolesByUserId(userId);
    }

    @Override
    public PageVo<SysRoleVo> getPageUserList(PageVo pageVo,String roleName) {
        int count = baseMapper.selectCount(new QueryWrapper<>());
        List<SysRole>  sysUserVO =  RoleMapper.getPageUserList(pageVo,roleName);
        pageVo.setRecords(sysUserVO);
        pageVo.setTotal(count);
        return  pageVo;
    }

    @Override
    public boolean add(SysRoleAddVo roleVo) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(roleVo,sysRole);

        if ( baseMapper.insert(sysRole) == 0){
            throw new FxjException(21,"角色插入失败");
        }
        return true;
    }

    @Override
    public boolean updateInfo(SysRoleVo roleVo) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(roleVo,sysRole);
        int i = baseMapper.updateById(sysRole);
        return 0 >= i ;
    }


    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deployRoleResources(Integer roleId, String ids) {
        roleResourcesService.removeById(roleId);
        if (StringUtils.isEmpty(ids)){
            return;
        }else{
            ArrayList<SysRoleResources> resources = Lists.newArrayList();
            List<Integer> list = LevelUtil.splitToListInt(ids);
            for (Integer id :list){
                SysRoleResources roleResources = new SysRoleResources();
                roleResources.setRoleId(roleId);
                roleResources.setResourcesId(id);
                resources.add(roleResources);
            }
            if (resources.size() > 0){
            roleResourcesService.saveBatch(resources);
            }
        }

    }


}
